package com.ihr360.payroll.controller.salaryreport;

import com.ihr360.commons.entity.AuthUserInfo;
import com.ihr360.commons.lang.DateUtils;
import com.ihr360.commons.vo.PageData;
import com.ihr360.maven.plugin.ModuleCodeStore;
import com.ihr360.maven.plugin.annotation.FunctionDefinition;
import com.ihr360.maven.plugin.annotation.ModuleDefinition;
import com.ihr360.payroll.model.dto.salary.report.embedded.SalaryFieldSnapshot;
import com.ihr360.payroll.model.response.salaryreport.BankPlateResponse;
import com.ihr360.payroll.request.salaryReport.SalaryReportRequest;
import com.ihr360.payroll.service.payperiod.StaffPayPeriodTermSalaryService;
import com.ihr360.payroll.service.salaryreport.HistorySalaryPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * Created by Eric on 2018/4/11/011.
 */
@Api("银行导盘接口")
@RestController
@RequestMapping(value = "/salaryBankPlate")
@ModuleDefinition(mainModuleCode = ModuleCodeStore.Module.SALARY_CODE,
        mainModuleName = ModuleCodeStore.Module.SALARY_NAME,
        subModuleCode = ModuleCodeStore.Module.Salary.BANK_PLATE_CODE,
        subModuleName = ModuleCodeStore.Module.Salary.BANK_PLATE_NAME,
        hasExtendsAbstractRestController = false)
public class BankPlateController {

    @Autowired
    private StaffPayPeriodTermSalaryService staffPayPeriodTermSalaryService;

    @Autowired
    private HistorySalaryPlanService historySalaryPlanService;


    @FunctionDefinition(functionName = "银行导盘查询", functionCode = ModuleCodeStore.Function.VIEW)
    @ApiOperation(value = "/salaryBankPlate/bankPlate", notes = "银行导盘查询")
    @PostMapping(value = "/bankPlate", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public PageData<BankPlateResponse> bankPlate(AuthUserInfo authUserInfo, @RequestBody SalaryReportRequest reportRequest) {
        return staffPayPeriodTermSalaryService.queryBankPlate(authUserInfo.getCompanyId(), reportRequest);
    }

    @FunctionDefinition(functionName = "导出银行导盘", functionCode = ModuleCodeStore.Function.EXPORT)
    @ApiOperation(value = "/salaryBankPlate/exportBankPlate", notes = "导出银行导盘")
    @GetMapping(value = "/exportBankPlate")
    public void exportBankPlate(AuthUserInfo authUserInfo, @RequestParam(required = false) Long termId, @RequestParam String fieldCode,
                                @RequestParam(required = false) Long mergeReportId, @RequestParam(required = false) Integer indexno,
                                @RequestParam(required = false) Boolean enableReport,@RequestParam(required = false) String termDate,
                                HttpServletResponse response) throws IOException {
        response.setHeader("x-filename", URLEncoder.encode("导盘数据.xls", "UTF-8"));
        SalaryReportRequest reportRequest = new SalaryReportRequest();
        if (enableReport == null || !enableReport) {
            reportRequest.setTermId(termId);
        } else {
            reportRequest.setMergeReportId(mergeReportId);
            reportRequest.setIndexno(indexno);
            reportRequest.setEnableReport(enableReport);
            reportRequest.setTermDate(DateUtils.parse(termDate));
        }
        reportRequest.setSalaryFieldCode(fieldCode);
        staffPayPeriodTermSalaryService.exportBankPlate(authUserInfo.getCompanyId(), reportRequest, response.getOutputStream());
    }

    @FunctionDefinition(functionName = "薪资方案所有字段", functionCode = ModuleCodeStore.Function.EXPORT)
    @ApiOperation(value = "/salaryReport/queryFieldList", notes = "返回薪资方案所有字段")
    @PostMapping(value = "/queryFieldList")
    public List<SalaryFieldSnapshot> queryFieldList(AuthUserInfo authUserInfo, @RequestBody SalaryReportRequest reportRequest) {
        return historySalaryPlanService.queryFieldList(authUserInfo.getCompanyId(), reportRequest);
    }
}
